package org.example.trainTicketSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.trainTicketSystem.entity.User;
import org.example.trainTicketSystem.mapper.UserMapper;
import org.example.trainTicketSystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;

@EnableCaching
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Cacheable(key = "#username", value = "userCache")
    @Override
    public User selectUserByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return userMapper.selectOne(queryWrapper);
    }

    @Cacheable(key = "#page + '-' + #size", value = "userPageCache")
    @Override
    public Page<User> selectAllUser(int page,int size) {
        Page<User> userPage = new Page<>(page,size);
        return userMapper.selectPage(userPage,null);
    }

}
